package cn.dida.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.dida.exception.BizzException;
import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.Pageable;
import cn.dida.model.Pbc;
import cn.dida.model.UserSession;
import cn.dida.service.PbcService;
/**
 * 箱规管理
 * @author weiwei
 *
 */
@Controller
public class PbcController extends BaseController {
    @Autowired
    private PbcService pbcService;

    /**
     * 箱规列表
     * @param request
     * @param pcount 每箱数量
     * @param state 状态（1：正常、2：已停用）
     * @param page 分页游标
     * @return ModelAndView 箱规列表页面
     * @throws Exception
     */
    @GetMapping("/pbc")
    public ModelAndView pbc(HttpServletRequest request, Integer pcount, Integer state, Integer page) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        page = (page == null || page < 1) ? 1 : page;
        
        Pageable<Pbc> pbcList = pbcService.queryPbc(userSession.getCompanyId(), pcount, state, page);
        model.put("pbcList", pbcList);
        model.put("pcount", pcount);
        model.put("state", state);
        return new ModelAndView("pbc_list").addAllObjects(model);
    }

    
    /**
     * 获取箱规列表数据
     * @param request
     * @param state 状态（1：正常、2：已停用）
     * @return HttpResponseBodyWrapper 包装箱规列表数据
     * @throws Exception
     */
    @ResponseBody
    @GetMapping(value = "/pbc", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResponseBodyWrapper getPbcList(HttpServletRequest request, Integer state) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);
        List<Pbc> pbcList = pbcService.queryPbc(userSession.getCompanyId(), state);
        model.put("pbcList", pbcList);
        return new HttpResponseBodyWrapper(pbcList);
    }

    /**
     * 新增箱规
     * @param request
     * @param pbc 箱规实体信息
     * @return HttpResponseBodyWrapper 封装操作标记（ true新增成功，false新增失败）
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("/pbc")
    public HttpResponseBodyWrapper postPbc(HttpServletRequest request, @RequestBody Pbc pbc) throws Exception {
        /*Map<String, Object> model = new HashMap<String, Object>();*/
        UserSession userSession = getUserSession(request);

        pbc.setCompanyId(userSession.getCompanyId());
        pbc.setCreateStaffId(userSession.getId());

        if (pbc.getPcount() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入规格数量");
        }
        if (pbcService.getPbcByPcount(userSession.getCompanyId(), pbc.getPcount()) != null) {
            throw new BizzException(BizzException.error_code_invalid_params, "已存在规格【1 × " + pbc.getPcount() + "】");
        }

        Boolean result = pbcService.insertPbc(pbc);
        return new HttpResponseBodyWrapper(result);
    }

    /**
     * 根据id获取箱规信息
     * @param request
     * @param id 箱规id
     * @return HttpResponseBodyWrapper 封装单个箱规实体数据
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/pbc/{id}")
    public HttpResponseBodyWrapper getPbc(HttpServletRequest request, @PathVariable Integer id) throws Exception {
       /* Map<String, Object> model = new HashMap<String, Object>();*/
        UserSession userSession = getUserSession(request);
        Pbc result = pbcService.getPbc(userSession.getCompanyId(), id);
        return new HttpResponseBodyWrapper(result);
    }

    /**
     * 更新箱规
     * @param request
     * @param pbc 箱规实体信息
     * @return HttpResponseBodyWrapper 封装操作标记（ true更新成功，false更新失败）
     * @throws Exception
     */
    @ResponseBody
    @PutMapping("/pbc/{id}")
    public HttpResponseBodyWrapper putPbc(HttpServletRequest request, @RequestBody Pbc pbc) throws Exception {
        /*Map<String, Object> model = new HashMap<String, Object>();*/
        UserSession userSession = getUserSession(request);

        pbc.setCompanyId(userSession.getCompanyId());
        if (pbc.getPcount() == null) {
            throw new BizzException(BizzException.error_code_invalid_params, "请输入规格数量");
        }
        Pbc pbc1 = pbcService.getPbcByPcount(userSession.getCompanyId(), pbc.getPcount());
        if (pbc1 != null && pbc1.getId().intValue() != pbc.getId().intValue()) {
            throw new BizzException(BizzException.error_code_invalid_params, "已存在规格【1 × " + pbc.getPcount() + "】");
        }

        Boolean result = pbcService.updatePbc(pbc);
        return new HttpResponseBodyWrapper(result);
    }

    /**
     * 删除箱规
     * @param request
     * @param id 箱规id
     * @return HttpResponseBodyWrapper 封装操作标记（ true删除成功，false删除失败）
     * @throws Exception
     */
    @ResponseBody
    @DeleteMapping("/pbc/{id}")
    public HttpResponseBodyWrapper deletePbc(HttpServletRequest request, @PathVariable Integer id) throws Exception {
       /* Map<String, Object> model = new HashMap<String, Object>();*/
        UserSession userSession = getUserSession(request);
        Boolean result = pbcService.deletePbc(userSession.getCompanyId(), id);
        return new HttpResponseBodyWrapper(result);
    }
}
